/*
*
	客户 任务量调整
*/
public with sharing class TaskAdjustController {

	public String backId{get; private set;}
	public String msg {get; set;}
	public Boolean isErr {get; private set;}
	public String dataJson {get; private set;}
	public String searchJson {get;  set;}

    public TaskAdjustController() {
        backId = System.currentPageReference().getParameters().get('id');
    }
    public void setNil(){
    	dataJson = '{}';
    	searchJson  ='{}';
    }

    //初始化
    public void init(){

    	try {

    		if(String.isBlank(backId))new DmlException('页面打开异常!');
    		if(Id.valueOf(backId).getSObjectType() == Account.SObjectType){
    			//新建调整

    			Map<String,Object> dataMap = new Map<String,Object>();
    			dataMap.put('terminals', getTeminalCustomer());
    			dataMap.put('regent', getGroupByDetail());
    			dataJson = EncodingUtil.urlEncode(JSON.serialize(dataMap), 'UTF-8');


    		}
    	    
    	}
    	catch (Exception e) {
    	    isErr = true;
    	    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()+e.getStackTraceString()));

    	}
    	
    	
    }
    //获取终端客户
    private Map<String,String> getTeminalCustomer(){

    	Map<String,String> accMap =new Map<String,String>();
    	for (CooperationStandard__c c : [select Id ,TerminalCustomer__c ,TerminalCustomer__r.Name  from CooperationStandard__c where Account__c	 =:backId ]) {
    		accMap.put(c.TerminalCustomer__c, c.TerminalCustomer__r.Name);	
    	}

    	return accMap;
    }

    //获取一级分类,二级分类,品牌
    private Map<String,Set<String>> getGroupByDetail(){
    	Map<String,Set<String>> m = new Map<String,Set<String>>();
    	m.put('cate1',new Set<String>() );
    	m.put('cate2',new Set<String>() );
    	m.put('brand',new Set<String>() );

    	for(AggregateResult arg : [select Category1__c	,Category2__c	,Brand__c from TaskAmount__c where Standers__r.Account__c =:backId Group by Category1__c	,Category2__c	,Brand__c ]){

    		m.get('cate1').add((String)arg.get('Category1__c'));
    		m.get('cate2').add((String)arg.get('Category2__c'));
    		m.get('brand').add((String)arg.get('Brand__c'));

    	}
    	return m; 

    }


    //检索
    public void Search(){

    	Map<String,Object> searchMap = (Map<String,Object>)JSON.deserializeUntyped(EncodingUtil.urlDecode(searchJson , 'UTF-8'));


    	//处理开始 截止时间 
    	String startDate = (String)searchMap.get('startDate');

    	List<String> ss = startDate.split('-');
    	Integer sY =Integer.valueOf(ss[0]);
    	Integer sM = Integer.valueOf(ss[1]);

    	List<String> sMs  = new List<String>();
    	for(Integer i = sM ; i <=12 ; i ++){
    		sMs.add(String.valueOf(i));
    	}
    	
    	String sql = 'select Year__c ,Category1__c	,Category2__c	,Brand__c, SUM(AssignAmount__c) AssignAmount__c,Standers__r.TerminalCustomer__c ,Standers__r.TerminalCustomer__r.Name  from TaskAmount__c where Standers__r.Account__c =\''+backId+'\' and NotActive__c != true   ';

   //  	String ymsql =  '  (( Year__c = \''+sY+'年\'  and Month__c in (\''+String.join(sMs, '\',\'') +'\')  )  or ( Year__c = \''+eY+'年\'  and Month__c in (\''+String.join(eMs, '\',\'')+'\')  )) ' ;
   //  	if(!years.isEmpty()){
   //  		sql += ' and ( Year__c in (\''+String.join(years, '\',\'')+'\')  or  '+ymsql+' ) ';
   //  	}else{
			// sql += ' and ' + ymsql;
   //  	}
    	sql += ' and  ( Year__c >\''+sY+'年\' or   ( Year__c = \''+sY+'年\'  and Month__c in (\''+String.join(sMs, '\',\'') +'\') ) )';




    	


    	if(String.isNotBlank((String)searchMap.get('terminalId'))) sql += ' and Standers__r.TerminalCustomer__c =\''+searchMap.get('terminalId')+'\' ';
    	 if(String.isNotBlank((String)searchMap.get('cate1'))) sql += ' and Category1__c =\''+searchMap.get('cate1')+'\' ';
    	if(String.isNotBlank((String)searchMap.get('cate2'))) sql += ' and Category2__c =\''+searchMap.get('cate2')+'\' ';
    	if(String.isNotBlank((String)searchMap.get('brand'))) sql += ' and Brand__c =\''+searchMap.get('brand')+'\' ';

    	sql += ' Group by Year__c,Category1__c	,Category2__c	,Brand__c,Standers__r.TerminalCustomer__c ,Standers__r.TerminalCustomer__r.Name ';

    	System.debug(sql);

    	List<AdjustBean> tmp = new List<AdjustBean>();

    	for(AggregateResult arg : Database.query(sql)){

    		tmp.add(new AdjustBean(arg,startDate));
    	}
    	dataJson = EncodingUtil.urlEncode(JSON.serialize(tmp), 'UTF-8');


    }

    public class AdjustBean{
    	private String detailId ;
    	private String cate1 ; 
    	private String cate2 ; 
    	private String brand ; 
    	private String terminalId;
    	private String terminalName;
    	//调整任务量
    	private Decimal adAmount ;
    	private Decimal originAmount ;
    	private String  startDate; //开始日期
    	private String  endDate ; //截止日期

    	private String year; //年度
    	private Integer moths ;//月数

    	public  AdjustBean(){

    	} 

    	public  AdjustBean( AggregateResult arg,String s  ){

    		cate1 = (String)arg.get('Category1__c');
    		cate2 = (String)arg.get('Category2__c');
    		brand = (String)arg.get('Brand__c');

    		terminalId = (String)arg.get('TerminalCustomer__c');
    		terminalName = (String)arg.get('Name');
    		adAmount = 0;
			originAmount = (Decimal)arg.get('AssignAmount__c') ;
			startDate = s;
			year = (String)arg.get('Year__c');

			List<String> ss = startDate.split('-');
	    	Integer sY =Integer.valueOf(ss[0]);
	    	Integer sM = Integer.valueOf(ss[1]);


	    	if(year == sY+'年'){

	    		moths = 12- sM +1 ;

	    	}else{

	    		moths = 12;

	    	}






    	}


    }

    
}